
package main;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Calendar;

/**
 * @author jp
 */
public class ReportCreator extends MIDlet implements CommandListener {

    Display display;
    Form form;
    Command exit;
    Command save;

    RecordStore rs;
    private final int QTY=1,
                      PRICE=2,
                      COST=3;

    public ReportCreator() {
        display=Display.getDisplay(this);
        
    }

    public void startApp() {
        setForm();
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if(c==exit) {
            destroyApp(false);
            notifyDestroyed();
        }
        if(c==save) {
            saveReport();
        }
    }
    private void setForm() {
        form=new Form("Сохранение отчётов:");
        exit=new Command("Выход", Command.EXIT, 1);
        save=new Command("Сохранить", Command.OK, 1);
        form.addCommand(exit);
        form.addCommand(save);
        form.setCommandListener(this);
        for(int i=0; i<RecordStore.listRecordStores().length; i++) {
            String storeName=RecordStore.listRecordStores()[i];
            form.append(storeName + "\n");
        }
    }

    private void saveReport() {
        Calendar cal=Calendar.getInstance();
        String fileName=String.valueOf(cal.get(cal.DAY_OF_MONTH))+"l"+
                        String.valueOf(cal.get(cal.MONTH)+1) +
                        String.valueOf(cal.get(cal.YEAR)) +
                        String.valueOf(cal.get(cal.HOUR_OF_DAY)) +
                        String.valueOf(cal.get(cal.MINUTE)) + ".txt";
        FileConnection fcon;
        try {
        fcon=(FileConnection)
                Connector.open(
                "file:///C:/predefgallery/predefphotos/" + fileName);
        fcon.create();
        OutputStream os=fcon.openOutputStream();
        OutputStreamWriter out=new OutputStreamWriter(os, "UTF8");
        if(RecordStore.listRecordStores()!=null) {
            try{
            for(int i=0; i<RecordStore.listRecordStores().length; i++) {
                String storeName=RecordStore.listRecordStores()[i];
                rs=RecordStore.openRecordStore(storeName, false);
                String price=new String(rs.getRecord(PRICE), "UTF8");
                String qty=new String(rs.getRecord(QTY), "UTF8");
                String cost=new String(rs.getRecord(COST), "UTF8");
                String toAppend=
                        storeName + " (" + qty + ")\n" + price + " руб."
                        + cost;
                
                out.write(toAppend, 0, toAppend.length());
                rs.closeRecordStore(); //??????????????????????
            }
        } catch(Exception e) {}
        } else {
            
        }
        out.close();
        os.close();
        fcon.close();
        } catch(Exception e) {}
    }
}

